#!/bin/bash
function delay()
{
	for i in 1 2 3 4 5
	do
		echo -n "."
		sleep 1
	done
}
OLD_CUR_DIR=${PWD}
DIR=/home/user/muclumon
cd ${DIR}
INSTALL=install
TOMCAT=tomcat
TOMCAT_BIN=${TOMCAT}/bin
MUCLUMON=${TOMCAT}/webapps/muclumon
SVN=svn
SVN_URL=https://muclumon.googlecode.com/svn/trunk/
SVN_USERNAME=Gleb.Rybakov
SVN_PASSWORD=bE6Jp3je8Yx7
VERSION=0.0.1-SNAPSHOT

function tomcat_shutdown()
{
	
	cd ${TOMCAT_BIN}
	chmod +x ./shutdown.sh
	./shutdown.sh
	cd ../..
	delay
	echo "DONE"
}

function tomcat_startup()
{
	cd ${TOMCAT_BIN}
	chmod +x ./shutdown.sh
	./startup.sh
	cd ../..
	delay
	echo "DONE"
}


# CLEAN
echo "[!] CLEAN"

if [ -d ${TOMCAT_BIN} ];
then
	echo "[!] forcing Tomcat to stop"
	tomcat_shutdown
fi

echo -n "[!] removing temporary files..."
if [ -d ${SVN} ]; then
	rm -R -f ${SVN}
fi
if [ -d ${TOMCAT} ]; then
	rm -R -f ${TOMCAT}
fi
echo "DONE"

# PRE-INSTALL
echo "[!] PRE-INSTALL"
echo -n "[!] installing Tomcat..."
tar -xf ${INSTALL}/apache-tomcat-6.0.20.tar.gz
mv apache-tomcat-6.0.20 ${TOMCAT}
echo "DONE"
echo -n "[!] installing Axis2 as muclumon..."
cp ${INSTALL}/muclumon.war ${TOMCAT}/webapps/
echo "DONE"
echo "[!] deploying Axis2:"
tomcat_startup
tomcat_shutdown
rm -f ${TOMCAT}/webapps/muclumon.war
echo "DONE"

# COMPILE
echo "[!] checkin out sources from SVN:"
sleep 1
svn --username ${SVN_USERNAME} --password ${SVN_PASSWORD} co ${SVN_URL} ${SVN}

echo "[!] compiling:"
sleep 1
export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=256m"
cd ${SVN}/muclumon-core
mvn install
cd ../muclumon-msvs
mvn install
cd ../muclumon-msvs-proxy
mvn install
cd ../muclumon-pes
mvn install
cd ../muclumon-server
mvn install
cd ../..

# INSTALL
echo "[!] INSTALL"

echo "[!] migrating Axis2 libraries to Tomcat:"
cp ${MUCLUMON}/WEB-INF/lib/* ${TOMCAT}/lib/
rm -R -f ${MUCLUMON}/WEB-INF/lib

#echo "[!] installing libraries"
#cp ${MUCLUMON}/ultima-core/lib/* ${TOMCAT}/lib/

echo -n "[!] installing module: muclumon-core..."
cp ${SVN}/muclumon-core/target/muclumon-core-${VERSION}.jar ${TOMCAT}/lib/
echo "DONE"
echo -n "[!] installing module: muclumon-msvs..."
cp ${SVN}/muclumon-msvs/target/muclumon-msvs-${VERSION}.jar ${TOMCAT}/lib/
echo "DONE"
echo -n "[!] installing module: muclumon-msvs-proxy..."
cp ${SVN}/muclumon-msvs-proxy/target/muclumon-msvs-proxy-${VERSION}.jar ${TOMCAT}/lib/
echo "DONE"
echo -n "[!] installing module: muclumon-pes..."
cp ${SVN}/muclumon-pes/target/muclumon-pes-${VERSION}.jar ${TOMCAT}/lib/
echo "DONE"
echo -n "[!] installing module: muclumon-server..."
cp ${SVN}/muclumon-server/target/muclumon-server-${VERSION}.jar ${TOMCAT}/lib/
echo "DONE"

# DATABASE
echo "[!] DATABASE"
echo -n "[!] importing database: muclumon..."
mysql -uroot -p123456 < ${SVN}/muclumon-core/etc/sql/muclumon.sql
echo "DONE"

#echo -n "[!] overriding server.xml..."
#cp -f ${SVN}/muclumon-core/etc/server.xml ${TOMCAT}/conf/
#echo "DONE"

echo "[!] starting Tomcat:"
tomcat_startup
cd ${OLD_CUR_DIR}